﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EzDesk.Utility.Web.Extensions
{
    public static class DateTimeForWebExtensions
    {
        /// <summary>
        /// 类似 3-2 17:32 的格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string FormattedDatetime(this DateTime date)
        {
            return String.Format(
                "{0}-{1} {2}:{3}",
                date.Month.ToString(),
                date.Day.ToString(),
                date.Hour.ToString("D2"),
                date.Minute.ToString("D2")
                );
        }

        /// <summary>
        /// 类似 2010-3-21 12:04 格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string FormattedFullDateTime(this DateTime date)
        {
            return String.Format(
                "{0}-{1}-{2} {3}:{4}",
                date.Year.ToString("D4"),
                date.Month.ToString("D2"),
                date.Day.ToString("D2"),
                date.Hour.ToString("D2"),
                date.Minute.ToString("D2")
                );
        }

        /// <summary>
        /// 类似 3-2 的格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string FormattedDate(this DateTime date)
        {
            return String.Format(
                "{0}-{1}",
                date.Month.ToString(),
                date.Day.ToString()
                );
        }

        /// <summary>
        /// 类似 17:32 的格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string FormattedTime(this DateTime date)
        {
            return String.Format(
                "{0}:{1}",
                date.Hour.ToString("D2"),
                date.Minute.ToString("D2")
                );
        }



        /// <summary>
        /// 类似 2004-02-01 的格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string FormattedFullDate(this DateTime date)
        {
            return String.Format(
                "{0}-{1}-{2}",
                date.Year.ToString("D4"),
                date.Month.ToString("D2"),
                date.Day.ToString("D2")
                );
        }

        /// <summary>
        /// 类似 星期五 的格式
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static string GetDayOfWeekStr(this DateTime date)
        {
            string dayOfWeekStr = null;
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    dayOfWeekStr = "星期日";
                    break;
                case DayOfWeek.Monday:
                    dayOfWeekStr = "星期一";
                    break;
                case DayOfWeek.Tuesday:
                    dayOfWeekStr = "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    dayOfWeekStr = "星期三";
                    break;
                case DayOfWeek.Thursday:
                    dayOfWeekStr = "星期四";
                    break;
                case DayOfWeek.Friday:
                    dayOfWeekStr = "星期五";
                    break;
                case DayOfWeek.Saturday:
                    dayOfWeekStr = "星期六";
                    break;
            }
            return dayOfWeekStr;
        }


        public static string FormattedTimeSpan(this TimeSpan diff)
        {
            StringBuilder result = new StringBuilder();

            if (diff.Days > 0)
            {
                result.AppendFormat("{0} 天", diff.Days);
            }

            if (diff.Hours > 0)
            {
                if (result.Length > 0)
                {
                    result.Append(", ");
                }

                result.AppendFormat("{0} 小时", diff.Hours);
            }

            if (diff.Minutes > 0)
            {
                if (result.Length > 0)
                {
                    result.Append(", ");
                }

                result.AppendFormat("{0} 分钟", diff.Minutes);
            }

            if (result.Length == 0)
            {
                result.Append("一会儿");
            }
            else
            {
                result.Insert(0, "大约 ");
            }

            return result.ToString();
        }
    }
}
